package com.cx.controller.wjj;

/**
 * 王晶晶
 */

import com.cx.pojo.wjr.UserPage;
import com.cx.service.wjj.AdminPageService;
import com.cx.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("admin")
public class AdminPageController {
    @Autowired
    AdminPageService adminPageService;


    @RequestMapping("pageAdd")
    public ResponseResult addPage(ResponseResult responseResult, UserPage userPage) {
        Integer pageNum=(int)(System.currentTimeMillis()%1000000000);
        Integer sendId=(int)(System.currentTimeMillis()%10000);
        Integer receiveId=(int)(System.currentTimeMillis()%10000);
        Integer pageCode=(int)(System.currentTimeMillis()%1000000);
        userPage.setPageNum(pageNum);
        userPage.setSendId(sendId);
        userPage.setReceiveId(receiveId);
        userPage.setPageCode(String.valueOf(pageCode));
        userPage.setStatus("运输中");
        responseResult = adminPageService.addPage(userPage);
        return responseResult;
    }
    @RequestMapping("pageUpdate")
    public ResponseResult updatePage(ResponseResult responseResult,UserPage userPage) {
            responseResult = adminPageService.updatePage(userPage);

        return responseResult;

    }
    @RequestMapping("pageDelete")
    public ResponseResult deletePage(ResponseResult responseResult,Integer pageNum) {

         responseResult = adminPageService.deletePage(pageNum);
        return responseResult;
    }

    @RequestMapping("pageSelectAll")
    public ResponseResult selectAll(ResponseResult responseResult) {
        responseResult =  adminPageService.selectAll();
        return responseResult;
    }
    @RequestMapping("pageSelectByMulti")
    public ResponseResult selectByMulti(ResponseResult responseResult,String searchInput) {
//        UserPage userPage = new UserPage();
        if(null == searchInput||"" == searchInput){
          responseResult = selectAll(responseResult);
          return responseResult;
        }
        String phoneRule = "^[\\d]{11}$";
        Pattern phonePattern = Pattern.compile(phoneRule);
        Matcher phoneMatcher = phonePattern.matcher(searchInput);

        String pageNumRule ="^[\\d]{9}$";
        Pattern pageNumPattern = Pattern.compile(pageNumRule);
        Matcher pageNumMatcher = pageNumPattern.matcher(searchInput);
        if(phoneMatcher.matches()){

           responseResult = adminPageService.selectByPhone(searchInput);


        }else if(pageNumMatcher.matches()){
          responseResult =  adminPageService.selectByPageNum(Integer.valueOf(searchInput));

        }else {

            responseResult = adminPageService.selectByName(searchInput);

        }
        return responseResult;
    }
    @RequestMapping("selectByAny")
    public ResponseResult selectByAny(ResponseResult responseResult,UserPage userPage){
        System.out.println(userPage);
       responseResult = adminPageService.selectByAny(userPage);
        return responseResult;
    }
}
